﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using JeanFish.Data.Common;
using System.Diagnostics;
using JeanFish.Tracing;

namespace JeanFish.Data.Tracing
{
    public class TracingCommand:DbCommand
    {
        private static int globalCommandID;

        public TracingCommand(System.Data.Common.DbCommand command, DbCommandDefinition commandDefinition)
            :base(command,commandDefinition)
        {
            this.CommandID = Interlocked.Increment(ref globalCommandID);
        }
        public int CommandID { get; private set; }
        private new TracingConnection Connection
        {
            get { return (TracingConnection)base.Connection; }
        }
        public override object ExecuteScalar()
        {
            Stopwatch watch = new Stopwatch();
            try
            {
                watch.Start();
                object result = base.ExecuteScalar();
                watch.Stop();
                return result;
            }
            catch (Exception ex)
            {
                ex.WriteLog(0);
                throw;
            }
        }
        public override int ExecuteNonQuery()
        {
            Stopwatch watch = new Stopwatch();
            try
            {
                watch.Start();
                int result = base.ExecuteNonQuery();
                watch.Stop();
                return result;
            }
            catch (Exception ex)
            {
                ex.WriteLog(0);
                throw;
            }
        }

        protected override System.Data.Common.DbDataReader ExecuteDbDataReader(System.Data.CommandBehavior behavior)
        {
            Stopwatch watch = new Stopwatch();
            try
            {
                watch.Start();
                System.Data.Common.DbDataReader result = base.ExecuteDbDataReader(behavior);
                watch.Stop();
                return result;
            }
            catch (Exception ex)
            {
                ex.WriteLog(0);
                throw;
            }
        }
    }
}
